package com.bw.zhuangao5___day002.UI.Login;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvp.V.BannerFragment;
import com.bw.zhuangao5___day002.Activity.MainActivity;
import com.bw.zhuangao5___day002.Glide.Glides;
import com.bw.zhuangao5___day002.JavaClass.ZhangAndReister;
import com.bw.zhuangao5___day002.R;
import com.bw.zhuangao5___day002.SQLline.SQLcontion;
import com.bw.zhuangao5___day002.Activity.JanewyerActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class BlankFragmentLogin extends BannerFragment implements LoginContion.loginview {

    Timer timer;
    int shijian =10;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==100)
            {
                shijian++;
                if (shijian==100)
                {

                }
            }
        }
    };
    ZhangAndReister user;
    private SharedPreferences sharedPreferences;
    private ImageView BlankFragmentLoginImageView;
    public static EditText BlankFragmentLoginEditText1;
    public static EditText BlankFragmentLoginEditText2;
    private CheckBox BlankFragmentLoginCheckBox1;
    private CheckBox BlankFragmentLoginCheckBox2;
    private CheckBox BlankFragmentLoginCheckBox3;
    private Button BlankFragmentLoginLogin;
    private Button BlankFragmentLoginReister;
    LoginPretenter pretenter;
    @Override
    public int bandLayoud() {
        return R.layout.fragment_blank_fragment_login;
    }

    @Override
    public void initView() {

        BlankFragmentLoginImageView = (ImageView) findViewById(R.id.BlankFragmentLogin_ImageView);
        BlankFragmentLoginEditText1 = (EditText) findViewById(R.id.BlankFragmentLogin_EditText1);
        BlankFragmentLoginEditText2 = (EditText) findViewById(R.id.BlankFragmentLogin_EditText2);
        BlankFragmentLoginCheckBox1 = (CheckBox) findViewById(R.id.BlankFragmentLogin_CheckBox1);
        BlankFragmentLoginCheckBox2 = (CheckBox) findViewById(R.id.BlankFragmentLogin_CheckBox2);
        BlankFragmentLoginCheckBox3 = (CheckBox) findViewById(R.id.BlankFragmentLogin_CheckBox3);
        BlankFragmentLoginLogin = (Button) findViewById(R.id.BlankFragmentLogin_Login);
        BlankFragmentLoginReister = (Button) findViewById(R.id.BlankFragmentLogin_Reister);
        pretenter = new LoginPretenter(this,new LoginModel());
        //TODO 读取
        sharedPreferences = getActivity().getSharedPreferences("1609A", Context.MODE_PRIVATE);
        boolean ischeck = sharedPreferences.getBoolean("ischeck", false);
        boolean isauto = sharedPreferences.getBoolean("isauto", false);
        if (ischeck) {
            //读到用户名和密码展现在页面中,复选框被勾选
            String username1 = sharedPreferences.getString("username", "");
            String password1 = sharedPreferences.getString("password", "");
            BlankFragmentLoginEditText1.setText(username1);
            BlankFragmentLoginEditText2.setText(password1);
            BlankFragmentLoginCheckBox2.setChecked(true);
        }
        if (isauto) {//自动登录被选中
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }
    }


    @Override
    public void initDate() {

        BlankFragmentLoginCheckBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                {
                    ToastUtils.showShort("自动挡有危险请谨慎驾驶！！！");
                    BlankFragmentLoginCheckBox2.setChecked(true);
                }
            }
        });

        BlankFragmentLoginCheckBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                {
                    ToastUtils.showShort("当前老司机速度已保存！！！");
                }
            }
        });

        BlankFragmentLoginLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username2 = BlankFragmentLoginEditText1.getText().toString().trim();
                String password2 = BlankFragmentLoginEditText2.getText().toString().trim();
                SQLcontion sqLcontion = new SQLcontion(getActivity(),"ShuJu.db",null,1);
                SQLiteDatabase writableDatabase = sqLcontion.getWritableDatabase();
                Cursor sqsq = writableDatabase.query("ShuJu", null, null, null, null, null, null);
                List<ZhangAndReister> kaixinbas = new ArrayList<>();
                while (sqsq.moveToNext()) {
                    String name = sqsq.getString(sqsq.getColumnIndex("zhanghao"));
                    String pasw = sqsq.getString(sqsq.getColumnIndex("mima"));
                    ZhangAndReister kaixinba = new ZhangAndReister(name, pasw);
                    kaixinbas.add(kaixinba);
                }

                if (!TextUtils.isEmpty(username2)&&!TextUtils.isEmpty(password2)){
                    boolean iskao = false;
                    for (int i = 0; i < kaixinbas.size(); i++) {
                        user = kaixinbas.get(i);
                        if (username2.equals(user.getZhanghao()) && password2.equals(user.getMima())) {
                            iskao = true;
                            //判断记住密码是否被选中
                            if (BlankFragmentLoginCheckBox2.isChecked()) {//存储数据
                                SharedPreferences.Editor edit = sharedPreferences.edit();
                                edit.putBoolean("ischeck", true);
                                edit.putString("username", username2);
                                edit.putString("password", password2);
                                edit.commit();
                            } else {//更新ischecked为false
                                SharedPreferences.Editor edit = sharedPreferences.edit();
                                edit.putBoolean("ischeck", false);
                                edit.commit();
                            }
                            //判断自动登录是否被选中
                            if (BlankFragmentLoginCheckBox3.isChecked()) {//存储数据
                                SharedPreferences.Editor edit = sharedPreferences.edit();
                                edit.putBoolean("isauto", true);
                                edit.commit();
                            } else {//更新isauto为false
                                SharedPreferences.Editor edit = sharedPreferences.edit();
                                edit.putBoolean("isauto", false);
                                edit.commit();
                            }

                            break;
                        }else {
                            iskao=false;
                        }
                    }

                    //-----------------------------开始------------------------------------------------------------------
                    if (iskao)
                    {
                        Log.i("数据",""+kaixinbas.size());
                        hor_progress_dialog();

                    }else {
                        Log.i("数据",""+kaixinbas.size());
                        Toast.makeText(getActivity(), "输入的车型或车牌号不对", Toast.LENGTH_SHORT).show();
                    }
                    //-------------------------结束----------------------------------------------------------------------

                }else {
                    Toast.makeText(getActivity(), "请老司机输入完整信息", Toast.LENGTH_SHORT).show();
                }

            }
        });

        Glides.Yuanjiao(getActivity(),R.drawable.dong2,BlankFragmentLoginImageView,10);
        BlankFragmentLoginReister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JanewyerActivity.day1();
            }
        });

        //-----------------------------------------------------------
        BlankFragmentLoginCheckBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                {
                    BlankFragmentLoginEditText2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    BlankFragmentLoginEditText2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                BlankFragmentLoginEditText2.setSelection(BlankFragmentLoginEditText2.length());
            }
        });

    }

    //水平进度条
    private void hor_progress_dialog() {
        //TODO 1:实例化对象
        final ProgressDialog dialog=new ProgressDialog(getActivity());
        //TODO 2：设置属性
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置水平央视
        dialog.setMax(100);
        dialog.setIcon(R.mipmap.shoucang);
        dialog.setTitle("老司机请等待正在启动中~~~~");
        //TODO 3:显示
        dialog.show();
        //TODO 4：模拟定时器
        final Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            int progress=0;
            @Override
            public void run() {
                if(progress==100){
                    dialog.dismiss();//消失
                    timer.cancel();
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }
                dialog.setProgress(progress+=50);
            }
        },0,1000);//0秒后执行，每隔1000毫秒执行一次
    }


    @Override
    public void getloginview(LoginClass loginClasses) {
        if (loginClasses.getCode()==0)
        {
            ToastUtils.showShort(""+loginClasses.getMsg());
            Intent intent  = new Intent(getActivity(),MainActivity.class);
            startActivity(intent);
        }else {
            ToastUtils.showShort(""+loginClasses.getMsg());
        }

    }
}
